package com.cx.base.autoDoc.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自动生成接口文档--工具类
 * 
 * @author cenxi
 * @date 2015-11-11
 */
public class FileUtil {
	
	

	/**
	 * 获取文件路径
	 * */
	public static String getFilePath(){
		//获取文件路径
		String path = FileUtil.class.getClass().getResource("/").getPath();
		path = path.substring(1);
		return path;
	}
	
	/**
	 * 获取文件路径
	 * */
	public static String getFilePath(Class<?> clazz){
		//获取文件路径
		String path = clazz.getClass().getResource("/").getPath();
		path = path.substring(1);
		path = path.replace("test-classes", "classes");
		path = path +"com";
		return path;
	}
	
	/**
	 * 获取类路径
	 * */
	public static String getClassPath(String projectName){
		//获取文件路径
		String path = getFilePath();
		path = path.replace("cloud_base", projectName);
		
		return path;
	}
	
	
	/**
	 * 
	 * array[i].getPath()
	 * */
	public static List<Map<String, String>> getFile(String path){   
        
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		
        File file = new File(path);   
        
        File[] array = file.listFiles();   
        if(array != null){
        	 for(int i=0;i<array.length;i++){
                 if(array[i].isFile()){
                	 HashMap<String, String> map = new HashMap<String, String>();
                	 String temp = array[i].getPath().substring(array[i].getPath().indexOf("\\com\\")+1);
                	 temp = temp.replaceAll("\\\\", ".");
                	 temp = temp.replace(".class", "");
                	 map.put("classPath", temp);
                	 map.put("path", array[i].getPath());
                	 list.add(map);
                 }else if(array[i].isDirectory()){   
                	 List<Map<String, String>> tempList = getFile(array[i].getPath());
                	 if(tempList != null && tempList.size() > 0){
                		 for(Map<String, String> map : tempList){
                			 list.add(map);
                		 }
                	 }
                 }   
             } 
        }
        return list;
    }
	
	public static Boolean delFile(String path) {
		File f = new File(path);
		// 输入要删除的文件位置
		if (f.exists())
			f.delete();
		return true;
	}
}
